//用苹果电脑的构建来理解建造者模式
#include<iostream>
#include<memory>
#include<string>
//1. 定义一个电脑类

class Computer
{
    protected:
    std::string board_;//主板
    std::string display_;//显示器
    std::string os_;//操作系统
    public:
    void setBoard(const std::string&board)
    {
        //主板的构建
        board_=board;
    }
    void setDisplay_(const std::string&display)
    {
        //显示器的构建
        display_=display;
    }
   virtual void setOs()//virtual 虚函数
    {
        //操作系统的构建
        //不同电脑的操作系统不一样
    }
    void Paramaters()
    {
        std::string paramaters="Paramaters:\n";
        paramaters+="\r电脑主板："+board_;
        paramaters+="\n\r电脑显示器："+display_;
        paramaters+="\n\r电脑系统："+os_;
        std::cout<<paramaters<<std::endl;
    }
};

class MacComputer : public Computer
{
    private:
    public:
    void setOs() override
    {
        os_="macOS Sequoia 15.6.1";
    }
};

//建造者类，建造者就是用来建造一个个零部件
class Builder
{
    private:
    public:
    virtual void buildboard(const std::string&board)=0;
    virtual void builddisplay(const std::string&display)=0;
    virtual void buildos()=0;
    virtual std::shared_ptr<Computer> build()=0;
};

//苹果电脑建造者
class MacBookBuilder:public Builder
{
    private:
    std::shared_ptr<Computer> computer_;//管理电脑的智能指针
    public:
    MacBookBuilder():computer_(new MacComputer())
    {

    }
    virtual void buildboard(const std::string&board)
    {
        computer_->setBoard(board);
    }
    
    void builddisplay(const std::string&display)
    {
        computer_->setDisplay_(display);
    }
    
    virtual void buildos()
    {
        computer_->setOs();//派生类对象调用派生类的setOs 
    }
    std::shared_ptr<Computer> build() override
    {
        return computer_;
    }
};

//指挥者
class Director
{
    private:
    std::shared_ptr<Builder> builder_;//指挥者是个builder？
    public:
    Director(Builder*builder):builder_(builder)//传入想要指挥的建造者
    {

    }
    void construct(const std::string&board,const std::string&display)
    {
        builder_->buildboard(board);
        builder_->builddisplay(display);
        builder_->buildos();
    }
};

int main()
{
    Builder*builder=new MacBookBuilder();
    std::unique_ptr<Director> director(new Director(builder));
    director->construct("华硕主板","宏基显示器");
    std::shared_ptr<Computer> computer=builder->build();
    computer->Paramaters();
    return 0;
}